/**
 *====================================================
 * 文件名称: BulkstockCollectionMaterialServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月28日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.service;

import java.util.ArrayList;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.bulkstock.entity.BulkstockCollectionMaterialEntity;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: BulkstockCollectionMaterialServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月28日 09:47:12
 */
@Implements(BulkstockCollectionMaterialService.class)
public class BulkstockCollectionMaterialServiceImpl implements BulkstockCollectionMaterialService {

	@Override
	@Transactional
	public Long createCollectionMaterial(BulkstockCollectionMaterialEntity entity) {
		entity.setCreateDatetime(DateUtils.now());
		entity.setEnabled(Boolean.TRUE);
		PersistUtil.insert(entity);
		return entity.getBulkstockCollectionMaterialId();
	}

	@Override
	public void updateCollectionMaterial(BulkstockCollectionMaterialEntity entity) {
		BulkstockCollectionMaterialEntity persist = inquireCollectionMaterialDetail(entity.getBulkstockCollectionMaterialId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	@Transactional
	public void disableCollectionMaterial(Long bulkstockCollectionMaterialId) {
		BulkstockCollectionMaterialEntity e = inquireCollectionMaterialDetail(bulkstockCollectionMaterialId);
		e.setEnabled(Boolean.FALSE);
		PersistUtil.update(e);
	}

	@Override
	public BulkstockCollectionMaterialEntity inquireCollectionMaterialDetail(Long collectionMaterialId) {
		return PersistUtil.get(BulkstockCollectionMaterialEntity.class, collectionMaterialId);
	}

	@Override
	public PageList<BulkstockCollectionMaterialEntity> inquireCollectionMaterialList(Integer startDate, Integer endDate, String region, String collector, PageCriteria page) {
		DetachedCriteria dc = DetachedCriteria.forClass(BulkstockCollectionMaterialEntity.class).add(Restrictions.eq("enabled", Boolean.TRUE));
		if (startDate != null) {
			dc.add(Restrictions.ge("collectionDate", startDate));
		}
		if (endDate != null) {
			dc.add(Restrictions.le("collectionDate", startDate));
		}
		if (StringUtil.isNotBlank(region)) {
			dc.add(Restrictions.like("region", "%" + region + "%"));
		}
		if (StringUtil.isNotBlank(collector)) {
			dc.add(Restrictions.like("collector", "%" + collector + "%"));
		}
		dc.addOrder(Order.desc("collectionDate"));
		return PersistUtil.get(dc, page);
	}

	@Override
	@Transactional
	public void callProcedure() {
		
		PersistUtil.callProcedure("PRO_FIN_BULKSTOCK_PRICE_CLEAN", new ArrayList<Object>(), new ArrayList<Object>());
		PersistUtil.callProcedure("PRO_FIN_BULKSTOCK_GATHER_CALCULATE", new ArrayList<Object>(), new ArrayList<Object>());
		PersistUtil.callProcedure("PRO_FIN_BULKSTOCK_COLLECTION_CALCULATE", new ArrayList<Object>(), new ArrayList<Object>());
		
		
	}

}
